﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DjModel;
using DjDal;
using System.Collections;
using System.Data;

namespace DjBll
{
    public class Term
    {

        private static object lockobj = new object();

        private static Dictionary<int, DjModel.TermInfo> termCatedic = null;
        /// <summary>
        /// 文章分类字典
        /// </summary>
        public static Dictionary<int, DjModel.TermInfo> TermCateDic
        {
            get
            {
                if (termCatedic == null)
                {
                    lock (lockobj)
                    {
                        if (termCatedic == null)
                        {
                            try
                            {
                                termCatedic = new Dictionary<int, TermInfo>();
                                using (DbHelper db = new DbHelper())
                                {
                                    IList<DjModel.TermInfo> termlist = DataProvider.GetTermList(db, 0);
                                    if (termlist != null)
                                    {
                                        foreach (DjModel.TermInfo term in termlist)
                                        {
                                            termCatedic.Add(term.TermID, term);
                                        }
                                    }
                                }
                            }
                            catch
                            {
                                termCatedic = null;
                            }
                        }
                    }
                }
                return termCatedic;
            }
        }

        private static Dictionary<int, DjModel.TermInfo> termTagdic = null;
        /// <summary>
        /// 文章标签字典
        /// </summary>
        public static Dictionary<int, DjModel.TermInfo> TermTagDic
        {
            get
            {
                if (termTagdic == null)
                {
                    lock (lockobj)
                    {
                        if (termTagdic == null)
                        {
                            try
                            {
                                termTagdic = new Dictionary<int, TermInfo>();
                                using (DbHelper db = new DbHelper())
                                {
                                    IList<DjModel.TermInfo> termlist = DataProvider.GetTermList(db, 1);
                                    if (termlist != null)
                                    {
                                        foreach (DjModel.TermInfo term in termlist)
                                        {
                                            termTagdic.Add(term.TermID, term);
                                        }
                                    }
                                }
                            }
                            catch
                            {
                                termTagdic = null;
                            }
                        }
                    }
                }
                return termTagdic;
            }
        }


        public static void ReloadDic()
        {
            termCatedic = null;
            termTagdic = null;
        }

        /// <summary>
        /// 得到分类下的所有文章(逗号隔开)
        /// </summary>
        /// <param name="tagid"></param>
        /// <returns></returns>
        public static string GetCatPost(int tagid)
        {
            string s = "";
            try
            {
                using (DbHelper db = new DbHelper())
                {
                    DataTable dt = DataProvider.GetPostIDTable(db, tagid.ToString());
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (i == 0)
                            {
                                s += dt.Rows[i]["post_ID"];
                            }
                            else
                            {
                                s += "," + dt.Rows[i]["post_ID"];
                            }
                        }
                    }
                }
            }
            catch
            {
                s = "";
            }
            return s;
        }


        /// <summary>
        /// 得到标签或分类的a标签html
        /// </summary>
        /// <param name="s"></param>
        /// <param name="cas"></param>
        /// <returns></returns>
        public static string GetTagHtml(string s, int cas)
        {
            string result = "";
            if (string.IsNullOrEmpty(s))
            {
                return result;
            }
            else
            {
                string[] cates = s.Split(',');
                for (int i = 0; i < cates.Length; i++)
                {
                    int _termid = Convert.ToInt32(cates[i]);
                    if (_termid > 0)
                    {
                        string name = cas == 0 ? (TermCateDic.ContainsKey(_termid) ? TermCateDic[_termid].TermName : "") : (TermTagDic.ContainsKey(_termid) ? TermTagDic[_termid].TermName : "");
                        result += string.Format("<a rel=\"{0}\" title=\"View all posts in {1}\" href=\"{2}\">{1}</a>　", cas == 0 ? "category" : "tag", name, cas == 0 ? DjCommon.Utils.GetTermUrl(_termid) : DjCommon.Utils.GetTagUrl(_termid));
                    }
                }
                return result;
            }
        }

        /// <summary>
        /// 添加分类　1成功2失败
        /// </summary>
        /// <param name="term"></param>
        /// <returns></returns>
        public static int AddTerm(TermInfo term)
        {
            int ret = -1;
            try
            {
                using (DbHelper db = new DbHelper())
                {
                    ret = DataProvider.AddTerm(db, term);
                    if (ret > 0)
                    {
                        ReloadDic();
                    }
                }
            }
            catch
            {
                ret = -1;
            }
            return ret;
        }

    }
}
